package com.LostAndFound.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.LostAndFound.domain.Article;
import com.LostAndFound.domain.FoundArticle;
import com.LostAndFound.domain.SystemInfo;
import com.LostAndFound.service.MessageService;
import com.LostAndFound.utils.CommonsUtils;

/*
 * 此MessageServlet.java已经弃用
 * 
 * */


public class MessageServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	MessageService service = new MessageService();
	
	public void getInfoList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 得到系统信息列表
		List<SystemInfo> systemInfoList = null;
		systemInfoList = service.getSystemInfoList();
		String uid = request.getParameter("uid");
		// 得到预约领取article列表
		List<Article> articleList = null;
		articleList = service.getArticleOrderList(uid);
		
		HttpSession session = request.getSession();
		session.setAttribute("systemInfoList", systemInfoList);
		session.setAttribute("articleList", articleList);
		
		response.setContentType("text/html;charset=utf-8");
		response.sendRedirect("user/03/myIssue.jsp");
	}

	// 展示预约领取的记录详情
	public void showArticleOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid =request.getParameter("aid");
		Map<String, Object> article = null;
		article = service.showArticleOrderDetail(aid);
		HttpSession session = request.getSession();
		session.setAttribute("article", article);
		response.sendRedirect("user/03/taken.jsp");
	}
	// 取消认领
	public void cancelOrderArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		boolean success = service.cancelOrderArticle(aid);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	// 确定认领
	public void confirmOrderArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		String foundInfo = request.getParameter("foundInfo");
		// 封装对象
		FoundArticle foundArticle = new FoundArticle();
		Article article = new Article();
		article.setAid(aid);
		foundArticle.setArticle(article);
		foundArticle.setFoundTime(CommonsUtils.getCurrentFormatTime());
		foundArticle.setFoundInfo(foundInfo);
		
		boolean success = service.confirmOrderArticle(foundArticle);
		
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	
	public void showSystemInfoDetailByInfoId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String infoid = request.getParameter("infoid");
		SystemInfo systemInfo = new SystemInfo();
		systemInfo = service.showSystemInfoDetailByInfoId(infoid);
		HttpSession session = request.getSession();
		session.setAttribute("systemInfo", systemInfo);
		response.sendRedirect("user/03/systemInfoDetail.jsp");
	}
}
